package cn.life.business.schedule.controller;

import cn.life.common.entity.model.*;
import cn.life.business.schedule.entity.po.Schedule;
import cn.life.business.schedule.entity.vo.ScheduleVo;
import cn.life.business.schedule.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author 王振宇
 * @Description: 工厂项目进度控制层
 * @Date 2018-07-13 17:21:41
 */
@RestController
@RequestMapping("schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    /**
     * @Description: 添加工厂项目进度记录
     * @param schedule
     * @return
     */
    @PostMapping(value = "/addschedule")
    public CMessage addschedule(@RequestBody Schedule schedule,@MyUser MyUserInfo myUserInfo){
        getOpr(schedule,myUserInfo);
        return  scheduleService.addschedule(schedule);
    }

    /**
     * @Description: 删除工厂项目进度记录
     * @param schedule
     * @return
     */
    @PostMapping(value = "/deleteschedule")
    public CMessage deleteschedule(@RequestBody Schedule schedule){
        return  scheduleService.deleteschedule(schedule);
    }

    /**
     * @Description: 修改工厂项目进度记录
     * @param schedule
     * @return
     */
    @PostMapping(value = "/updateschedule")
    public CMessage updateschedule(@RequestBody Schedule schedule){
        return  scheduleService.updateschedule(schedule);
    }

    /**
     * @Description: 查询进度列表
     * @param scheduleVo
     * @return
     */
    @PostMapping(value = "/getallscheduleinfo")
    public CMessage getAllScheduleInfo(@RequestBody ScheduleVo scheduleVo,@MyUser MyUserInfo myUserInfo){
        return  scheduleService.getAllScheduleInfo(scheduleVo,myUserInfo);
    }

    /**
     * @Description: 项目看板
     * @param scheduleVo
     * @return
     */
    @PostMapping(value = "/getProjectKanBan")
    public CMessage queryProjectKanban(@RequestBody ScheduleVo scheduleVo,@MyUser MyUserInfo myUserInfo){
        return  scheduleService.queryProjectKanban(scheduleVo,myUserInfo);
    }

    /**
     * @Description: ajax 查询
     * @param scheduleVo
     * @return
     */
    @PostMapping("/ajaxget")
    public CMessage ajaxAllScheduleInfo(@RequestBody ScheduleVo scheduleVo, HttpServletRequest request, HttpServletResponse response){
          return scheduleService.getAjax(scheduleVo);
          /*Entity entity=new Entity();
          entity.setList(list);
          PrintReponse.printResult(request,response,entity);*/
    }

    /**
     * @Description: 查询单条（详情页面）工厂项目进度记录
     * @param scheduleVo
     * @return
     */
    @PostMapping(value = "/getschedule")
    public CMessage getSchedule(@RequestBody ScheduleVo scheduleVo){
        return scheduleService.getSchedule(scheduleVo);
    }


    @PostMapping(value = "/audit")
    public CMessage auditSchedule(@RequestBody ScheduleVo scheduleVo,@MyUser MyUserInfo myUserInfo){
        scheduleVo.setOprUserId(myUserInfo.getUserId());
        return scheduleService.auditSchedule(scheduleVo);
    }

    /**
     * @Description:  自动添加项
     * @param   schedule,myUserInfo
     * @return
     */
    public  Schedule  getOpr(Schedule schedule, MyUserInfo myUserInfo){
        schedule.setOprUserId(myUserInfo.getUserId());
        schedule.setOprUserName(myUserInfo.getUserName());
        schedule.setOprDate(new Date());
        if(schedule.getProjectId() == null){
            schedule.setProjectId(myUserInfo.getProjectId());
        }
        return schedule;
    }
}
